<html>

<head>
	<meta charset="utf-8">
  	<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
	<script src="http://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
	<script src="https://npmcdn.com/axios/dist/axios.min.js"></script>
</head>

<body>

	<h1>API Admin Test</h1>
	<h3>- DB Service Test Page</h3>
	<h3>- MySQL Server </h3>

	<br>
	<br>
	<h3>1. Login</h3>
	<form id="loginForm" onsubmit="return false;">
		id:<input type="text" name="username" value="tim" />
		pw:<input type="password" name="passwd" value="sausages" />
		<input type="checkbox" id="isEncryptLogin" name="encryptLogin[]" value="1" />is encrypt js
		<input type="button" value="Sign In" onclick="signIn(this.form.username.value, this.form.passwd.value)"/>
	</form>
	
	<div id="login">
		<div id="token">Current token:</div>
	</div>
	<br>
	<br>
	
	<h3>2. Create User With Salt</h3>
	<div id="createUser">
		id:<input type="text" id="create_username" name="create_username" value="" />
		pw:<input type="password" id="create_password" name="create_password" value="" />
		<input type="checkbox" id="isEncrypt" name="encrypt[]" value="1" />is encrypt js
		<a href="#" id="createUserTest">USER Create Execute</a>
	</div>
	
	<h3>3. API CRUD TEST</h3>
	<div id="read">
		<a href="#" id="readtest">Read Test</a>
	</div>

	<div id="create">
		<a href="#" id="createtest">Create Test</a>
	</div>

	<div id="update">
		<a href="#" id="updatetest">Update Test</a>
	</div>

	<div id="delete">
		<a href="#" id="deletetest">Delete Test</a>
	</div>
	
	<h3>4. API BATCH CRUD TEST</h3>
	<div id="insertBatch">
		<a href="#" id="insertBatchTest">Insert Batch Test</a>
	</div>

	<script src="./sha256.js" type="text/javascript" charset="UTF-8"></script>
	<script type="text/javascript" charset="UTF-8">
		
	var urlHeaderStr = location.protocol + '//' + location.host;
		// axios test function 
		function axiosTest() {
			
			var payload = {
					username: "kkkkkk",
					password: SHA256("123456")
				};
			axios.post( urlHeaderStr + '/api/newToken', payload)
		    .then( response => { console.log(response) } )
		    .catch( response => { console.log(response) } );
		}
		
		
		
		
		
		function signIn(argUsername, argPasswd) {
			var inputPw;
			var isSeasonChk = $("input:checkbox[name='encryptLogin[]']").is(":checked");
	    	
	        if(!isSeasonChk) inputPw = argPasswd;
	        else inputPw = SHA256(argPasswd);
	        
			var payload = {
				username: argUsername,
				password: inputPw
			};
			var url = location.protocol.concat("//", location.host, "/api/newToken");

			var xhr = new XMLHttpRequest();

			xhr.open("POST", url, true);
			xhr.setRequestHeader('Content-type', 'application/json; charset=UTF-8');
			xhr.responseType = "text";
			xhr.onload = function (event) {
				if (xhr.status == 200) {
					console.log(xhr.response);
					$('#token').html('Current Token: ' + xhr.response);
					localStorage.setItem("com.hansol.pom.pomadmin.jwttoken", xhr.response);
				} else {
					console.log(event);
				}
			}

			xhr.send(JSON.stringify(payload));
		}
		$(document).ready(function () {
			
			
			// Read API Test
			$('#readtest').on('click', function () {
				var payload = {
					"sqlName": "sql_read_test",
					"user_id": "kkkkkk"
				};
				// xhttp.setRequestHeader('X-PINGARUNER', 'pingpong');
				var header = {
						  headers: {
							  "Authorization": "Bearer " + localStorage.getItem("com.hansol.pom.pomadmin.jwttoken"),
							  "X-PINGARUNER": "pingpong"
						  }
						};
				axios.post(urlHeaderStr + '/api/read', payload, header)
			    .then( response => { console.log(response) } )
			    .catch( response => { console.log(response) } );
				
				/*
				$.ajax({
					url: '/api/read',
					type: 'POST',
					dataType: 'json',
					contentType: 'application/json; charset=utf8',
					data: JSON.stringify(payload),
					headers: {
						"Authorization": "Bearer " + localStorage.getItem("com.hansol.pom.pomadmin.jwttoken")
					},
					success: function (res) {
						console.log(JSON.stringify(res));
					},
					error: function (err) {
						var contentType = err.getResponseHeader("content-type");
						console.log('contentType : ' + contentType);
						console.log('Error: ' + err.toString());
						console.log(JSON.parse(err.responseText).error);
					}
				});*/
			});
			
			
			// Create User Test
			$('#createUserTest').on('click', function () {
				var inputPw;
		        var isSeasonChk = $("input:checkbox[name='encrypt[]']").is(":checked");
		    	
		        if(!isSeasonChk) inputPw = $('#create_password').val();
		        else inputPw = SHA256($('#create_password').val());

				var payload = {
						sqlName: 'sql_user_create',
						username: $('#create_username').val(),
						password: inputPw
					};

					$.ajax({
						url: '/api/usercreate',
						type: 'POST',
						dataType: 'json',
						contentType: 'application/json; charset=utf8',
						data: JSON.stringify(payload),
						headers: {
							"Authorization": "Bearer " + localStorage.getItem("com.hansol.pom.pomadmin.jwttoken")
						},
						success: function (res) {
							console.log(res);
						},
						error: function (err) {
							console.log('Error: ' + err.toString());
						}
					});
			});
			
			
			// Create API Test
			$('#createtest').on('click', function () {

				var payload = {
					sqlName: 'sql_insert_test',
					data1: 'aa',
					data2: 'bb',
					data3: 'cc'
				};

				$.ajax({
					url: '/api/create',
					type: 'POST',
					dataType: 'json',
					contentType: 'application/json; charset=utf8',
					data: JSON.stringify(payload),
					headers: {
						"Authorization": "Bearer " + localStorage.getItem("com.hansol.pom.pomadmin.jwttoken")
					},
					success: function (res) {
						console.log(res);
					},
					error: function (err) {
						console.log('Error: ' + err.toString());
					}
				});
			});

			
			
			
			// Update API Test
			$('#updatetest').on('click', function () {

				var payload = {
					sqlName: 'sql_update_test',
					data1: 'aa',
					data2: 'xxx'
				};

				$.ajax({
					url: '/api/update',
					type: 'POST',
					dataType: 'json',
					contentType: 'application/json; charset=utf8',
					data: JSON.stringify(payload),
					headers: {
						"Authorization": "Bearer " + localStorage.getItem("com.hansol.pom.pomadmin.jwttoken")
					},
					success: function (res) {
						console.log(res);
					},
					error: function (err) {
						console.log('Error: ' + err.toString());
					}
				});
			});

			
			
			
			// Delete API Test
			$('#deletetest').on('click', function () {

				var payload = {
					sqlName: 'sql_delete_test',
					data1: 'aa',
				};

				$.ajax({
					url: '/api/delete',
					type: 'POST',
					dataType: 'json',
					contentType: 'application/json; charset=utf8',
					data: JSON.stringify(payload),
					headers: {
						"Authorization": "Bearer " + localStorage.getItem("com.hansol.pom.pomadmin.jwttoken")
					},
					success: function (res) {
						console.log(res);
					},
					error: function (err) {
						console.log('Error: ' + err.toString());
					}
				});
			});
			
			
			
			
			// Batch Create/Update/Delete API Test
			$('#insertBatchTest').on('click', function () {

				var payload = {
					sqlName: "sql_insert_batch_test",
					arr: [
						{ data1: "1", data2: "a" },
						{ data1: "2", data2: "b" },
						{ data1: "3", data2: "c" },
						{ data1: "4", data2: "d" },
						{ data1: "5", data2: "e" }
					]
				};

				$.ajax({
					url: '/api/create/multi',
					type: 'POST',
					dataType: 'json',
					contentType: 'application/json; charset=utf8',
					data: JSON.stringify(payload),
					headers: {
						"Authorization": "Bearer " + localStorage.getItem("com.hansol.pom.pomadmin.jwttoken")
					},
					success: function (res) {
						console.log(res);
					},
					error: function (err) {
						console.log('Error: ' + err.toString());
					}
				});
			});
		});
	</script>
</body>

</html>